<template>
    <div class="cate">
        <!-- 面包屑区域 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片区域 -->
        <el-card>
            <!-- 添加分类按钮 -->
            <el-row>
                <el-col>
                    <el-button type='primary' @click="addCate">添加分类</el-button>
                </el-col>
            </el-row>

            <!-- 表格区域 -->
            <tree-table :data="cateList"
                        :columns="columns"
                        :selection-type='false'
                        :expand-type="false"
                        show-index
                        index-text="#"
                        :show-row-hover='false'
                        class="treeTable"
                        border>
                
                <!-- 是否有效 -->
                <template slot='isOk' slot-scope="scope">
                    <i class="el-icon-circle-check" v-if="scope.row.cat_deleted===false" style="color:lightgreen"></i>
                    <i class="el-icon-circle-close" v-else style="color:red"></i>
                </template>

                <!-- 排序 -->
                <template slot='order' slot-scope="scope">
                    <el-tag type='primary' v-if="scope.row.cat_level===0">一级</el-tag>
                    <el-tag type='success' v-else-if="scope.row.cat_level===1">二级</el-tag>
                    <el-tag type='warning' v-else>三级</el-tag>
                </template>

                <!-- 操作 -->
                <template slot="opt" slot-scope="scope">
                    <el-button type='success' size='mini' icon="el-icon-edit"></el-button>
                    <el-button type='danger' size='mini' icon="el-icon-delete"></el-button>
                </template>
            </tree-table>

            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryInfo.pagenum"
                            :page-sizes="[1, 2, 3, 5]"
                            :page-size="queryInfo.pagesize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
            </el-pagination>
        </el-card>

        <!-- 添加分类区域 -->
        <el-dialog title="添加分类"
                :visible.sync="addCateDialogVisible"
                width="50%"
                @close="addCateClose">
            <el-form :model="addCateForm" 
                    :rules="addCateFormRules" 
                    ref="addCateFormRef" 
                    label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类">
                    <el-cascader v-model="selectKeys"
                                :options="parentCateList"
                                props.expandTrigger="hover"
                                :props="cascaderProps"
                                @change="parentCateChanged"
                                clearable
                                changee-on-select>
                    </el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addCateDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCateRight">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import request from 'network/query'
export default {
   name:'Cate',
   data(){
       return {
           queryInfo:{
               type:3,
               pagenum:1,
               pagesize:5
           },
           cateList:[],
           total:0,
           columns:[
               {label:'分类名称',prop:'cat_name'},
               {label:'是否有效',type: 'template',template: 'isOk'},
               {label:'分类名称',type: 'template',template: 'order'},
                {label:'分类名称',type: 'template',template: 'opt'},
           ],
        //    添加分类对话框的显示与隐藏
           addCateDialogVisible:false,
           addCateForm:{
               cat_name:'',
               cat_pid:0,
               cat_level:0
           },
           addCateFormRules:{
               cat_name:[
                   {required:true,message:'请输入分类名称',trigger:'blur'},
                   {min:3,max:8,message:'长度在3-8字符之间',trigger:'blur'}
               ]
           },
           parentCateList:[],
           cascaderProps:{
               value:'cat_id',
               label:'cat_name',
               children:'children'
           },
           selectKeys:[]
       }
   },
   created(){
       this.getCateList()
   },
   methods:{
    //    获取商品分类数据
       getCateList(){
           request('categories',this.queryInfo,{},'get').then(res=>{
               console.log(res);
               if(res.meta.status !== 200){
                   this.$message.error('获取分类商品失败')
               }
               this.cateList = res.data.result
               this.total = res.data.total
           })
       },

       handleSizeChange(newSize){
        //    console.log(newSize);
        this.queryInfo.pagesize = newSize
        this.getCateList()
       },

       handleCurrentChange(newPage){
        //    console.log(newPage);
        this.queryInfo.pagenum = newPage
        this.getCateList()
       },

       addCate(){
           this. getParentCateList()
           this.addCateDialogVisible = true
       },

       async getParentCateList(){
        //    request('categories',{type:2}).then(res=>{
        //        console.log(res);
        //        if(res.meta.status !==200){
        //            this.$message.error('获取父子分类失败')
        //        }
        //        this.parentCateList = res.data
        //        console.log(this.parentCateList);
        //    })
            const {data:res} = await this.$http.get('categories',{
                params:{type:2}
            })
            // console.log(res.data);
            this.parentCateList = res.data
        //    console.log(this.parentCateList);
       },

       parentCateChanged(){
        //    console.log(this.selectKeys);
           if(this.selectKeys.length > 0){
               this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length-1]
               this.addCateForm.cat_level = this.selectKeys.length
               return
           }
           this.addCateForm.cat_level = 0
           this.addCateForm.cat_pid = 0
       },

       addCateRight(){
           this.$refs.addCateFormRef.validate(valid=>{
               if(!valid) return
               request('categories',{},this.addCateForm,'post').then(res=>{
                   console.log(res);
                   if(res.meta.status !==201){
                       this.$message.error('添加分类失败')
                   }
                   this.$message.success('添加分类成功')
                   this.getCateList()
               })
           })
           this.addCateDialogVisible = false
       },
       addCateClose(){
           this.$refs.addCateFormRef.resetFields()
           this.selectKeys = []
           this.addCateForm.cat_level = 0
           this.addCateForm.cat_pid = 0
       }
   }
};
</script>

<style scoped>
    .el-cascader{
        width: 100%;
    }
</style>
